-- 极致抗压技能
local LuaJizhikangya = fk.CreateSkill {
    name = "LuaJizhikangya",
    anim_type = "defensive",
    frequency = Skill.Limited,
}

Fk:loadTranslationTable{
    ["LuaJizhikangya"] = "极致抗压",
    ["@jizhikangya_times"] = "极致抗压",
    ["#LuaJizhikangya-recover"] = "极致抗压：你可以弃置一张牌令体力回复至1",
    ["#LuaJizhikangya-disable"] = "极致抗压：禁用所有5血及以上武将",
    [":LuaJizhikangya"] = "当你开通此武将时，你视为禁用所有5血及其以上武将。当你即将进入濒死状态时，你可以弃置一张牌令体力回复至1。",
}

-- 游戏开始时替换高血量武将
LuaJizhikangya:addEffect(fk.GameStart, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(LuaJizhikangya.name)
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        
        for _, p in ipairs(room.alive_players) do
            if p.maxHp >= 5 then
                -- 替换为士兵武将
                room:changeHero(p, "blank_shibing", true, false,true)
                room:broadcastProperty(p, "general")
            end
        end
    end,
})

LuaJizhikangya:addEffect(fk.EventAcquireSkill, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(LuaJizhikangya.name) and data == self
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        
        for _, p in ipairs(room.alive_players) do
            if p.maxHp >= 5 then
                -- 替换为士兵武将
                room:changeHero(p, "blank_shibing", true, false,true)
                room:broadcastProperty(p, "general")
            end
        end
    end,
})

-- 濒死时恢复体力
LuaJizhikangya:addEffect(fk.EnterDying, {
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(LuaJizhikangya.name) and not player:isNude()
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local card = room:askToDiscard(player, {
            min_num = 1,
            max_num = 1,
            include_equip = true,
            skill_name = LuaJizhikangya.name,
            prompt = "#LuaJizhikangya-recover",
            cancelable = true,
            skip = true,
        })
        if #card > 0 then
            event:setCostData(self, {cards = card})
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:throwCard(event:getCostData(self).cards, LuaJizhikangya.name, player, player)
        local recover_count = 1 - player.hp
        room:recover({
            who = player,
            num = recover_count,
            recoverBy = player,
            skillName = LuaJizhikangya.name
        })
        
        -- 记录累计恢复次数
        local recover_times = player:getMark("@jizhikangya_times")
        room:setPlayerMark(player, "@jizhikangya_times", recover_times + 1)
        
        -- 检查是否触发逆转大局
        if recover_times >= 3 then
            room:notifySkillInvoked(player, "LuaNizhuan")
                -- 获取所有存活玩家
            local alive_players = room:getAlivePlayers()
            -- 收集所有身份
            local roles = {}
            for _, p in ipairs(alive_players) do
                table.insert(roles, p.role)
            end
            -- 打乱身份顺序
            for i = #roles, 2, -1 do
                local j = math.random(i)
                roles[i], roles[j] = roles[j], roles[i]
            end
            -- 重新分配身份
            for i, p in ipairs(alive_players) do
                room:setPlayerProperty(p, "role", roles[i])
                room:broadcastProperty(p, "role")
            end
            -- 重置记录
            room:setPlayerMark(player, "@jizhikangya_times", 0)
            
        end
    end,
})

return LuaJizhikangya